package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.ProductReviews;
import com.example.mall.dto.ProductReviewsDto;
import com.example.mall.mapper.ProductReviewsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductReviewsQuery;
import com.example.mall.service.IProductReviewsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import lombok.AllArgsConstructor;
import org.omg.CORBA.OMGVMCID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品审核表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class ProductReviewsServiceImpl extends ServiceImpl<ProductReviewsMapper, ProductReviews> implements IProductReviewsService {

    @Autowired
    private ProductReviewsMapper productReviewsMapper;

    @Override
    public PageDTO<ProductReviewsDto> queryPage(ProductReviewsQuery productReviewsQuery) {
        if (productReviewsQuery == null) {
            productReviewsQuery = new ProductReviewsQuery();
        }
        Page<ProductReviews> page = productReviewsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ProductReviews> cartPage = lambdaQuery()
                .like(productReviewsQuery.getReviewerId()!= null, ProductReviews::getReviewerId, productReviewsQuery.getReviewerId())
                .like(productReviewsQuery.getProductId()!= null, ProductReviews::getProductId, productReviewsQuery.getProductId())
                .like(productReviewsQuery.getReviewId()!= null, ProductReviews::getReviewId, productReviewsQuery.getReviewId())
                .like(productReviewsQuery.getReviewComment()!= null, ProductReviews::getReviewComment, productReviewsQuery.getReviewComment())
                .like(productReviewsQuery.getReviewStatus()!= null, ProductReviews::getReviewStatus, productReviewsQuery.getReviewStatus())
                .orderByDesc(ProductReviews::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ProductReviewsDto.class);
    }

    @Override
    public ProductReviews queryProductReviewsById(Long reviewId) {
        AssertUtil.isNotNull(reviewId, "reviewId不能为空");
        return productReviewsMapper.selectById(reviewId);
    }

    @Override
    public List<ProductReviews> queryProductReviewsList() {
        return this.list();
    }

    @Override
    public void insert(ProductReviewsDto productReviewsDto) {
        ProductReviews productReviews = new ProductReviews();
        CopyUtils.copyProperties(productReviewsDto, productReviews);
        productReviews.setReviewId(CodeGenerateUtils.generateUniqueId());
        productReviews.setCreateDate(new Date());
        productReviews.setUpdateDate(new Date());
        productReviews.setReviewStatus(BaseConstants.NOT_USED);
        productReviewsMapper.insert(productReviews);
    }

    @Override
    public void update(ProductReviewsDto productReviewsDto) {
        AssertUtil.isNotNull(productReviewsDto.getReviewId(), "reviewId不能为空");
        ProductReviews productReviews = productReviewsMapper.selectById(productReviewsDto.getReviewId());
        AssertUtil.isNotNull(productReviews, "商品审核信息不存在");
        CopyUtils.copyProperties(productReviewsDto, productReviews);
        productReviews.setUpdateDate(new Date());
        productReviewsMapper.updateById(productReviews);
    }

    @Override
    public void delete(Long reviewId) {
        AssertUtil.isNotNull(reviewId, "reviewId不能为空");
        ProductReviews productReviews = productReviewsMapper.selectById(reviewId);
        AssertUtil.isNotNull(productReviews, "商品审核信息不存在");
        productReviews.setUpdateDate(new Date());
        productReviews.setReviewStatus(BaseConstants.EXPIRED);
        productReviewsMapper.updateById(productReviews);
    }

    @Override
    public void deleteBatch(List<Long> reviewIds) {
        AssertUtil.isNotNull(reviewIds, "reviewIds不能为空");
        List<ProductReviews> productReviews = productReviewsMapper.selectBatchIds(reviewIds);
        AssertUtil.isNotNull(productReviews, "商品审核信息不存在");
        for (ProductReviews productReview : productReviews) {
            productReview.setUpdateDate(new Date());
            productReview.setReviewStatus(BaseConstants.EXPIRED);
            productReviewsMapper.updateById(productReview);
        }
    }
}
